var express=require('express')
// 先引入第三方包
var app=express()

app.use(express.static('static'))
//在所有接口设置前处理Post请求参数的中间件
app.use( express.json())
app.use( express.urlencoded({extended:false}))


var arr=[
    {email:'3dfes@sjdrt.com	',name:'丽丽',phone:'123456789',pass:'1234'
    ,ingroup:'编辑组',isadmin:true,status:true},
    {email:'3dfes@sjdrt.com	',name:'木木',phone:'987645123',pass:'4321'
    ,ingroup:'编辑组',isadmin:true,status:true},
    {email:'3dfes@sjdrt.com	',name:'踢踢',phone:'564789123',pass:'4567'
    ,ingroup:'编辑组',isadmin:true,status:false}
    ]
//设置接口
app.get( '/user/list', (req,res)=>{

    var {page,count}=req.query
    //分页
    var newarr=arr.slice( (page-1)*count,page*count)
    res.send( { code:200,msg:'请求成功！',list:newarr})
})


//模糊搜索
app.get('/user/search',(req,res)=>{
    var {searchkey}=req.query;
    var newarr=arr.filter((item)=>{
        return item.name.includes(searchkey)
    })
    res.send({code:200,msg:'查询成功',list:newarr})
})


//登录
app.post('/user/login',(req,res)=>{
    var {phone,pass}=req.body
    var i=arr.findIndex((item,index)=>{
        return item.phone==phone&&item.pass==pass
    })
    console.log(phone,pass);
    if(i!=-1){
        res.send({code:200,msg:'登录成功',userinfo:arr[i]
    ,token:''})
    }else{
        res.send({code:400,msg:'登录失败'})
    }

    res.send( {code:200,msg:'登录成功！'})
    res.send( {code:400,msg:'登录失败！'})
})



//设置监听
app.listen(8080,()=>{
console.log('服务器运行在:http://localhost:8080');
})